package com.zgl.hello;

import com.zgl.hello.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class Activity_ex0315 extends Activity {
	private TextView textView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_0315);
		
		textView = (TextView)findViewById(R.id.textView1);
		textView.setTextColor(Color.CYAN);
		
		((Gallery)findViewById(R.id.gallery1)).setAdapter(new ImageAdapters(this));

		
		
		Button button = (Button)findViewById(R.id.btnItems);
		button.setOnClickListener(btnItemsListener);
	}
	
	Button.OnClickListener btnItemsListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			new AlertDialog.Builder(Activity_ex0315.this).setTitle("Choose an item:")
			.setItems(R.array.items_for_choose, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					CharSequence former = getString(R.string.you_like);
					String[] aryAnimal = getResources().getStringArray(R.array.items_for_choose);
					new AlertDialog.Builder(Activity_ex0315.this).setMessage(former + " " + aryAnimal[which])
					.setNeutralButton(R.string.yes, new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}
					}).show();
				}
			}).setNegativeButton("取消", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			}).show();
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		//MenuItem add(int groupId,int itemId,int order,CharSequence title)
		menu.add(0, 0, 0, R.string.about);
		menu.add(0, 1, 0, R.string.exit);
		return super.onCreateOptionsMenu(menu);
	};
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
			case 0:
				clickAbout();
				break;
			case 1:
				clickExit();
				break;
		}
		
		return true;
	}
	
	private void clickAbout(){
		AlertDialog.Builder aBuilder = new AlertDialog.Builder(this);
		aBuilder.setTitle("About This APP");
		aBuilder.setMessage("This is an App for showing picture, just a test.");
		aBuilder.setPositiveButton(R.string.btn, null).show();
	}
	
	private void clickExit(){
		AlertDialog.Builder aBuilder = new AlertDialog.Builder(this);
		aBuilder.setTitle("信息");
		aBuilder.setMessage("信息内容");
		aBuilder.setNegativeButton(R.string.yes, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				finish();
			}
		}).setPositiveButton(R.string.no, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		}).show();
		
	}
	
	public class ImageAdapters extends BaseAdapter{
		private Context context;
		private int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5
				, R.drawable.image6, R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10, R.drawable.image11
				, R.drawable.image12, R.drawable.image13, R.drawable.image14, R.drawable.image15, R.drawable.image16};
		
		public ImageAdapters(Context c){
			this.context = c;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.images.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(this.context);
			
			imageView.setImageResource(this.images[position]);
			imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			
			imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
			return imageView;
		}
		
	}

}
